<template>
  <div class="login-container">
    <div class="box" />
    <div class="sanjiao" />
    <div class="login-form">
      <el-form v-if="isRegister" ref="registerForm" :model="registerForm" :rules="rules" class="" auto-complete="on" label-position="left">
        <div class="title-container">
          <h3 class="title">注册账号</h3>
        </div>
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="registerForm.username"
            placeholder="请输入用户名"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="off"
          />
        </el-form-item>

        <el-form-item prop="email">
          <span class="svg-container">
            <svg-icon icon-class="email" />
          </span>
          <el-input
            ref="email"
            v-model="registerForm.email"
            placeholder="请输入邮箱"
            name="email"
            type="text"
            tabindex="1"
            auto-complete="off"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="registerForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            tabindex="2"
            auto-complete="off"
          />
          <span class="show-pwd" @click="showPwd(1)">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>

        <el-form-item prop="checkPass">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="checkPass"
            v-model="registerForm.checkPass"
            :type="passwordType"
            placeholder="请确认密码"
            name="password"
            tabindex="2"
            auto-complete="off"
          />
          <span class="show-pwd" @click="showPwd(2)">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>

        <el-button type="primary" class="login-btn" style="width:100%;" @click.native.prevent="handleRegister">注册</el-button>
      </el-form>

      <el-form v-else ref="loginForm" class="loginForm" :model="loginForm" :rules="rules" auto-complete="on" label-position="left">

        <div class="title-container">
          <h3 class="title">用户登陆</h3>
        </div>

        <el-form-item prop="identifier">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="identifier"
            v-model="loginForm.identifier"
            placeholder="请输入用户名或邮箱"
            name="identifier"
            type="text"
            tabindex="1"
            auto-complete="off"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="Password"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>

        <!--
        <div class="tips">
          <span style="margin-right:1.042vw;">username: admin</span>
          <span> password: any</span>
        </div> -->
        <el-row class="tips" type="flex" justify="space-between">
          <span style="margin-right:1.042vw;">
            <input type="checkbox" style="vertical-align:baseline;margin-right:0.26vw">七天内自动登陆 </span>
          <span>  忘记密码</span>
        </el-row>
        <el-button type="primary" class="login-btn" style="width:100%;" @click.native.prevent="handleLogin">登   陆</el-button>

      </el-form>
      <div v-if="!isRegister" class="endBtn" @click="isRegister = true">马上注册
        <i class="el-icon-caret-right" />
      </div>
      <div v-else class="endBtn" @click="isRegister = false">已有账号？去登陆 <i class="el-icon-caret-right" /></div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        identifier: 'admin',
        password: 'adminadmin'
      },
      registerForm: {
        username: '',
        email: '',
        password: '',
        checkPass: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        identifier: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 12, message: '密码长度3到12位', trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur'] }
        ]
      },
      passwordType: 'password',
      redirect: undefined,
      isRegister: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd(num) {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      if (num === 1) {
        this.$nextTick(() => {
          this.$refs.password.focus()
        })
      } else if (num === 2) {
        this.$nextTick(() => {
          this.$refs.checkPass.focus()
        })
      }
    },
    async handleLogin() {
      await this.$refs.loginForm.validate()
      await this.$store.dispatch('user/vuexLogin', this.loginForm)
      this.$router.push('/')
    },
    async handleRegister() {
      await this.$refs.registerForm.validate()
      await this.$store.dispatch('user/vuxRegister', this.registerForm)
      this.registerForm = {
        username: '',
        email: '',
        password: '',
        checkPass: ''
      }
      this.isRegister = false
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  // background-color: $bgc;
  .el-input {
    display: inline-block;
    height: 2.448vw;
    width: 85%;

    input {
      background: transparent;
      border: 0vw;
      -webkit-appearance: none;
      border-radius: 0vw;
      padding: 0.625vw 0.26vw 0.625vw 0.781vw;
      color: #000;
      height: 2.448vw;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0vw 52.083vw $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 0.052vw solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 0.26vw;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>

$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 27.083vw;
    max-width: 100%;
    padding: 8.333vw 1.823vw 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 0.729vw;
    color: #fff;
    margin-bottom: 0.521vw;

    span {
      &:first-of-type {
        margin-right: 0.833vw;
      }
    }
  }

  .svg-container {
    padding: 0.313vw 0.26vw 0.313vw 0.781vw;
    color: $dark_gray;
    vertical-align: middle;
    width: 1.563vw;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 1.354vw;
      color: $light_gray;
      margin: 0vw auto 2.083vw auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 0.521vw;
    top: 0.365vw;
    font-size: 0.833vw;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>

<style lang="scss" scoped>
.login-container{
  background-color: #244653;
  position: relative;
   .sanjiao{
      z-index: 99;
      display: inline-block;
      position: absolute;
      top:7.813vw;
      right:41.667vw;
      border-bottom:0.521vw solid ;
      width: 0;
      height: 0;
      border: 1.042vw solid transparent;
      border-width: 4.167vw 1.563vw 0 0;
      border-right-color: #FEDCAC;
    }
    .box{
      width: 52.083vw;
      min-height: 21.875vw;
      background: url('../../assets/img/loginbg1.jpg');
      background-size: 100% 100%;
      position: absolute;
      top: 11.979vw;
      // right: 41.667vw;
      right: 31.667vw;
    }
  .login-form{
    .login-btn{
      height: 2.604vw;
      font-size: 0.938vw;
      margin-bottom: 0;
     }
     .endBtn{
      margin-top: 1.042vw;
      color:#09958D;
      text-align: center;
      cursor: pointer;
     }
    .loginForm{
     .el-form-item{
        margin: 1.563vw 0 ;
     }
     .tips{
      color: #244653;
     }

    }
    position: absolute;
    top: 7.813vw;
    right: 18.229vw;
    padding: 2.083vw 1.563vw;

    background-color: #fff;
    width: 23.438vw;
    min-height: 28.646vw;
box-shadow: 0 0.104vw 0.208vw rgba(0, 0, 0, .12), 0 0 0.313vw rgba(0, 0, 0, .04);
  .title-container{
    .title{
      font-weight: normal;
      color:#3e4a58;
      margin-bottom: 2.604vw;
    }
  }
  .el-button--primary{
background-color: #244653;
  }
 }
}
</style>
